﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Whisper.CommonTypes;
using Whisper.Interfaces;

namespace Whisper.UI.Win.UserControls
{
    public partial class ListConversationsControl : UserControl, IRetrieveConversationClientView
    {
        public ListConversationsControl()
        {
            InitializeComponent();
           
        }

        public event EventHandler RetrieveAvailableSubscriptions;
        public IConversationList AvailableConversations
        {
            get
            {
               
                return MapToConversationList(this.AvailableConversationsListBox.Items);
            }
            set
            {
                 this.AvailableConversationsListBox.Items.Clear();
                this.AvailableConversationsListBox.ValueMember = "Name";
                this.AvailableConversationsListBox.DisplayMember = "Name";
                this.AvailableConversationsListBox.Items.AddRange(value.ToArray());
            }
        }

        private static IConversationList MapToConversationList(ListBox.ObjectCollection collection)
        {
            var conversations = new ConversationList();
            foreach (var c in conversations)
            {
                conversations.Add(new Conversation
                                      {
                                          Name = c.Name
                                      });
            }
            return conversations;          
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RetrieveAvailableSubscriptions(this,e);
        }
    }
}
